package com.lenovo.gpsautotest.LocationManager;

/**
 * @ProjectName: GpsAutoTest
 * @Package: com.lenovo.gpsautotest.LocationManager
 * @ClassName: GpsLocateTask
 * @Description: GpsLocateTask类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/6/3 10:57
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/6/3 10:57
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */


import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.lenovo.gpsautotest.Constant;
import com.lenovo.gpsautotest.LocationAssistant;
import com.lenovo.gpsautotest.LocationManager.EventBusEvents.ResultUpdateEvent;
import com.lenovo.gpsautotest.LocationResult;

import org.greenrobot.eventbus.EventBus;


/**
 * 执行Gps定位的异步任务
 */
public class GpsLocateTask extends AsyncTask<String, String, LocationResult> {
    private final String TAG = GpsLocateTask.class.getSimpleName();
    private int order;
    private int failOrder;
    private boolean isHot;
    private Context context;


    public GpsLocateTask(int order, int failOrder,boolean isHot, Context context) {
        this.order = order;
        this.failOrder=failOrder;
        this.isHot=isHot;
        this.context=context;
    }

    @Override
    protected void onPreExecute() {
        Log.d(TAG, "onPreExecute: ---------------------------------");
        super.onPreExecute();
    }

    @Override
    protected LocationResult doInBackground(String... strings) {
        Log.d(TAG, "doInBackground: -------------------------------");
        LocationResult mLocationResult = null;
        if (!isHot) {
            //冷启动需要先删除之前的定位数据
            LocationAssistant.deleteAidingData(context);
        }
        mLocationResult = LocationAssistant.locateByGPS(context);
        if (mLocationResult != null) {
            mLocationResult.setResult(Constant.PASS);
        } else {
            mLocationResult.setResult(Constant.FAIL);
        }
        mLocationResult.setOrder(order);
        mLocationResult.setFailOrder(failOrder);
        Log.d(TAG, "doInBackground: " + mLocationResult.toString());
        return mLocationResult;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        Log.d(TAG, "onProgressUpdate: -------------------------------");
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(LocationResult locationResult) {
        Log.d(TAG, "onPostExecute: ----------------------------");
        ResultUpdateEvent event=new ResultUpdateEvent();
        event.setMessage("GPS Location Finished-----");
        event.setLocationResult(locationResult);
        EventBus.getDefault().post(event);
    }

    @Override
    protected void onCancelled() {
        Log.d(TAG, "onCancelled: -----------------------------");
        super.onCancelled();
    }
}
